
import {_login,_loginOut} from 'network/login'
import {getCookie,setCookie,romoveCookie} from 'utils/auth.js'

const urser = {
	state: {
		cookie:getCookie(),
		avatar:'',
		uid:'',
		isloading:false,
		permission:[],
	},
	mutations:{
		SET_COOKIE:(state, cookie) =>{
			state.cookie = cookie 
		},
		SET_AVATAR:(state, avatar)=> {
			state.avatar =  avatar 
		},
		SET_UID:(state, uid)=> {
			state.uid = uid 
		},
		hiddenLoading(state){
		  state.isloading=false;
		},
		/**显示隐藏loading */
		showLoading(state){
		  state.isloading=true;
		},
		SET_PERMISSION:(state, permission) =>{
			state.permission = permission 
		},
	},
	actions:{
		// 登录
		Login({ commit }, userInfo){
			const email = userInfo.email.trim()
			const password = userInfo.password;
			return new Promise((resolve, reject) => {
			  _login(email, password).then(res => {
			    setCookie(res.data.cookie)
			    commit('SET_COOKIE', res.data.cookie)
				commit('SET_AVATAR', res.data.profile.avatarUrl)
				commit('SET_UID', res.data.profile.userId)
			    resolve()
			  }).catch(error => {
			    reject(error)
			  })
			})
		},
		// 退出
		LoginOut({ commit, state }){
			return new Promise((resolve, reject) => {
			  _loginOut().then(res => {
			    commit('SET_COOKIE', '')
				commit('SET_AVATAR', '')
				commit('SET_UID','')
				romoveCookie()
			    resolve()
			  }).catch(error => {
			    reject(error)
			  })
			})
		},
		// 前端 登出
		FedLogOut({ commit }) {
		  return new Promise(resolve => {
		    commit('SET_TOKEN', '')
		    removeToken()
		    resolve()
		  })
		},
		getPermission({ commit},data ){
			
			commit('SET_PERMISSION', data);
		}
	}
}

export default urser
